package com.bd.scm.module.jurisdiction.controller;

import com.bd.scm.domain.dto.jurisdiction.BdSysLoginUserDto;
import com.bd.scm.domain.models.jurisdiction.BdSysLoginUser;
import com.bd.scm.module.jurisdiction.service.BdSysLoginUserService;
import com.bd.scm.module.utils.ExamConstants;
import com.bd.utils.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/** 系统Controller
 * @author YH
 * @version 1.0
 * @date 2020/12/15 8:59
 */
@RestController
@RequestMapping("api/system")
public class SystemController {

    @Autowired
    private BdSysLoginUserService bdSysLoginUserService;

    /**
     * 登陆
     * @param bdSysLoginUserDto
     * @return
     */
    @PostMapping("dologin")
    public Result dologin(@RequestBody BdSysLoginUserDto bdSysLoginUserDto, HttpSession session) {
        Result result = null;
        try {
            BdSysLoginUser bdSysLoginUser = this.bdSysLoginUserService.dologin(bdSysLoginUserDto.getAccount(),bdSysLoginUserDto.getPassword());
            if(bdSysLoginUser == null){
                result = Result.error("用户名或密码错误！");
            }else{
                result = Result.ok("保存成功");
                session.setAttribute(ExamConstants.SESSION_USER,bdSysLoginUser);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("服务器内部错误，保存失败");
        }
        return result;
    }

    /**
     * 用户注销
     * @param session
     * @return
     */
    @PostMapping("logout")
    public Result logout(HttpSession session){
        session.invalidate();
        Result result = null;
        try {
            result = Result.ok("注销成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("服务器内部错误，保存失败");
        }
        return result;
    }


}
